package org.example.fancystudy.atomics;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chaigq
 * @version 1.0
 * @date 2023/2/17 16:59
 */
class MyNumber{
    AtomicInteger atomicInteger = new AtomicInteger();
    public void addPlusPlus() {
        atomicInteger.getAndIncrement();
    }
}

public class AtomicIntegerDemo {
    public static final int SIZE = 50;
    public static void main(String[] args) throws InterruptedException {
        MyNumber myNumber = new MyNumber();
        CountDownLatch countDownLatch = new CountDownLatch(SIZE);
        for (int i = 0; i < 1000; i++) {
            try{
                new Thread(() -> {
                    myNumber.addPlusPlus();
                },String.valueOf(i)).start();
            }finally {
                countDownLatch.countDown();
            }
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t"+"result: "+myNumber.atomicInteger.get());
    }
}
